home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18232 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  98 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: String operator+ and memory leakage.
  5. Date: Fri, 19 Apr 1996 15:16:06 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4l8am8$701@news.halcyon.com>
  8. References: <4l5foo$fen@utopia.hacktic.nl>
  9. NNTP-Posting-Host: blv-pm3-ip4.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Mike Tavares <MIKET@cdynamics.com> wrote:
  13.  
  14. >I have an implementation (not mine) of the String class that is leaking 
  15. >memory
  16. >during the + operator code.  The code follows:
  17.  
  18. >class String
  19. >    {
  20. >    public:
  21.  
  22. >        // Constructors/destructors
  23. >        String();
  24. >        String( char* new_string);
  25. >        String( String& new_string);
  26.  
  27. >        ~String()
  28. >            {
  29. >            delete character_array;
  30. >            }
  31.  
  32. >        String&     operator+( String& a_string );
  33. >        String&     operator=( String& a_string );
  34. >        operator    char* () { return character_array; }
  35.  
  36. >    private:
  37.  
  38. >        char* character_array;
  39. >        int string_length;
  40. >    };
  41.  
  42. >String&
  43. >String::operator+( String& a_string )
  44. >    {
  45. >    int total_size = string_length + strlen( a_string ) + 1;
  46. >    char* temp_array = new char[total_size];
  47. >    char* char_array = a_string;
  48.  
  49. >    strcpy( temp_array, character_array );
  50. >    strcat( temp_array, char_array );
  51.  
  52. >    String* new_string = new String( temp_array );
  53. >    delete [] temp_array;
  54. >    return *new_string;
  55. >    }
  56. > ...
  57. >My usage is:
  58.  
  59. >destString = string1 + string2 + string3...;
  60.  
  61. >The problem is, when new_string is created and returned through a 
  62. >reference, no one ever deletes it.
  63.  
  64. >If I change new_string to be an automatic variable I get an error from 
  65. >the compiler about trying to pass a reference to a item that is out of 
  66. >scope.
  67.  
  68. >If I change the method to work in this I mutate one of my addends.  There 
  69. >has to be a way of implementing this without memory leakage! HELP!
  70.  
  71. >TIA
  72.  
  73. >=MikeT
  74.  
  75. > ---------------------------------------------------------------------
  76. >| Mike Tavares         | miket@cdynamics.com    |                     |
  77.  
  78. This doesn't seem like a very well thought-out string class.
  79. operator+() should return a string, not a string &, I should think.
  80.  
  81. String  String::operator+( const String & aString ) const
  82. {
  83.     int totalsize = string_length + aString.string_length;
  84.     char * temp = new char[ totalsize ];
  85.     ... etc...
  86.     return String( temp );        
  87. }
  88.  
  89. The nameless automatic returned will go out-of-scope and clean-up
  90. automatically.  Returning a nameless object instead of a named object
  91. allows the compiler to potentially optimize the code so you may not
  92. end up calling a copy-ctor to go from operator+'s stack frame to the
  93. callee's stack-frame.   And, of course, no dangling news!
  94.  
  95.                         --Norm 
  96.  
  97.  
  98.